package com.haowan123.qinmeiren.appota2;

import com.android.mylib.Interface;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.Toast;

public class SplashScreen extends Activity {
	private Handler handler=new Handler();
	private Interface helper;
	private int stt;
	private ProgressDialog loading;
	private ImageView proLogo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen);
		helper= UIApp.getHelper();
		stt=UIApp.getSTT();
		Log.e("status0","stt:"+stt);
		loading = new ProgressDialog(this);
		loading.setMessage(getResources().getString(R.string.loading));
		loading.setCancelable(false);
		proLogo=(ImageView)findViewById(R.id.prologo);
		if(CommonUtils.checkNetworkAvaliable(this)){
			handler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					doGet();
				}
			}, 1500);
		}else{
			AlertDialog.Builder builder=new Builder(this);
			builder.setMessage(getResources().getString(R.string.check_network)).setPositiveButton(getResources().getString(R.string.retry), new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(CommonUtils.checkNetworkAvaliable(SplashScreen.this)){
						loading.show();
						doGet();
					}else{
						Toast.makeText(SplashScreen.this, getResources().getString(R.string.check_network), Toast.LENGTH_SHORT).show();
					}
				}
			}).setNegativeButton(getResources().getString(R.string.exit), new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					continueScreen();
				}
			}).setCancelable(false).show();
		}
		
	}
	private void doGet() {
		if(stt!=-1){
			UIApp.setSTT(stt);
			continueScreen();
		}else{
			stt=helper.init(CommonUtils.getDeviceId(this), getApplicationContext().getPackageName(), CommonUtils.sdCardPath());
			if(stt==-1){
				UIApp.setSTT(stt);
				//still wait for another 1500ms
				handler.postDelayed(new Runnable() {
					
					@Override
					public void run() {
						if(loading!=null && loading.isShowing()){
							loading.dismiss();
						}
						continueScreen();
					}
				}, 1500);
			}else{
				if(loading!=null && loading.isShowing()){
					loading.dismiss();
				}
				continueScreen();
			}
			
		}
	}
	
	private void continueScreen(){
		Animation fadeOut = new AlphaAnimation(1f, 0f);
        fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
//        fadeOut.setStartOffset(1000);
        fadeOut.setFillAfter(true);
        fadeOut.setDuration(3000);
        fadeOut.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				if(CommonUtils.isFileAvaiable(SplashScreen.this)){
					Intent intent=new Intent(SplashScreen.this,	FirstActivity.class);
					startActivity(intent);
				}else
				if(stt==1){
					Intent intent=new Intent(SplashScreen.this,	LoadingScreen.class);
					startActivity(intent);
				}else {
					Intent intent=new Intent(SplashScreen.this,	FirstActivity.class);
					startActivity(intent);
				}
				finish();
			}
		});
        proLogo.startAnimation(fadeOut);
        
	}

}
